#!/bin/bash
# $Id: echo_syscheck,v 1.4 2004/08/09 06:03:47 repeater Exp $
# $Log: echo_syscheck,v $
# Revision 1.4  2004/08/09 06:03:47  repeater
# Corrected ECHO_TBD_CMD to ECHO_TBD_COMMAND.
#
# Revision 1.3  2004/08/03 20:25:45  repeater
# Added "^" and "head -1" to ConferenceCall grep so only one line that begins with ConferenceCall is returned.
#
# Revision 1.2  2004/08/03 20:23:15  repeater
# Added -f to rm -f "$LOCAL"/echo_conference.
# Removed >> from end of a log statement.
#
# Revision 1.1  2004/08/03 19:28:24  repeater
# Initial revision
#

[ -z "$ECHO_SCRIPT" ] && ECHO_SCRIPT=/home/EchoIRLP/scripts
   source $ECHO_SCRIPT/echo_common

rm -f "$LOCAL"/echo_conference

CALLSIGN=`grep "^ConferenceCall =" /usr/local/etc/tbd.conf | head -1 | cut -f3 -d " "`

# Determines node type for access control
# check for conference first
echo "$CALLSIGN" > "$LOCAL"/echo_check
eval `awk '/*/ {print "TYPE=conference"}' "$LOCAL"/echo_check`
# Is this a link?
if [ "$TYPE" = "" ] ; then 
  eval `awk '/-L/ {print "TYPE=link"}' "$LOCAL"/echo_check`
fi
# Is this a repeater?
if [ "$TYPE" = "" ] ; then  
  eval `awk '/-R/ {print "TYPE=repeater"}' "$LOCAL"/echo_check`
fi

# Must be a Node number or Callsign
if [ "$TYPE" = "" ] ; then
  TYPE=user
fi

rm -f "$LOCAL"/echo_check

echo "${0##*/}: System type = $TYPE"
# Check if system configured as PC user and shut down tbd if this is the case.
if [ "$TYPE" = "user" ]; then
  #echo "System is misconfigured.  Must use a -L or -R EchoLink ID."
  #echo "Shuting down EchoIRLP..."
  TBD_COMMAND quit
  log "Node misconfigured with PC User ID.  Shutting down ..."
  exit 0
fi

# If tbd configured as conference, don't want to interact with IRLP node.
if [ "$TYPE" = "conference" ]; then
  #echo "System is configured as conference.  Disabling EchoIRLP RF link."
  touch "$LOCAL"/echo_conference
  log "tbd is configured as *CONFERENCE*.  Disabling EchoIRLP RF link."
  exit 0
fi

# OK, we're using an appropriate EchoLink ID for an RF gateway.
# Time for some sanity checks... :-)

if [ "$CALLSIGN" != "$ECHO_MYCALL" ]; then
  #echo "System is misconfigured.  Please check that ECHO_MYCALL is set to the"
  #echo "same callsign as ConferenceCall in tbd.conf"
  log "ECHO_MYCALL does not match callsign in tbd.conf!  Shutting down ..."
  exit 0
fi

if [ "$CALLSIGN" != `echo $ECHO_NODE_DESC | cut -f1 -d " "` ]; then
  #echo "System is misconfigured.  Please check that ECHO_NODE_DESC starts with the"
  #echo "same callsign as ConferenceCall in tbd.conf"
  log "ECHO_NODE_DESC does not start with callsign in tbd.conf!  Shutting down ..."
  exit 0
fi

if [ "$CALLSIGN" != `cat "$ECHO_CUSTOM"/userdata.txt | grep "$CALLSIGN"` ]; then
  #echo "System is misconfigured.  Please check that the callsign in userdata.txt is set to the"
  #echo "same callsign as ConferenceCall in tbd.conf"
  log "Callsign in userdata.txt does not match callsign in tbd.conf!  Shutting down ..."
  exit 0
fi


echo "${0##*/}: Callsign configuration OK!"
